﻿using System;
using System.IO;
using System.Text;

namespace NDBGen
{
    public class StringBuilderStream : Stream
    {
        public StringBuilderStream()
        {
            this.Builder = new StringBuilder();
        }

        public StringBuilder Builder { get; private set; }

        public override Boolean CanRead
        {
            get { return false; }
        }

        public override Boolean CanSeek
        {
            get { return false; }
        }

        public override Boolean CanWrite
        {
            get { return true; }
        }

        public override void Flush() { }

        public override Int64 Length
        {
            get { return this.Builder.Length; }
        }

        public override Int64 Position
        {
            get { throw new NotImplementedException(); }
            set { throw new NotImplementedException(); }
        }

        public override Int32 Read(Byte[] buffer, Int32 offset, Int32 count)
        {
            throw new NotImplementedException();
        }

        public override Int64 Seek(Int64 offset, SeekOrigin origin)
        {
            throw new NotImplementedException();
        }

        public override void SetLength(Int64 value)
        {
            this.Builder.Length = (Int32)value;
        }

        public override void Write(Byte[] buffer, Int32 offset, Int32 count)
        {
            var value = Encoding.Default.GetString(buffer, offset, count);
            this.Builder.Append(value);
        }

        public override String ToString()
        {
            return this.Builder.ToString();
        }
    }
}
